package org.jxc.service;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.dao.StockDAO;
import org.jxc.domain.InOrderProduct;
import org.jxc.domain.OutOrderProduct;
import org.jxc.domain.ProductWarehouseStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.dao.DAOFactory;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.SearchUtil;
import org.team4.vo.Page;

@Service
public class StockService implements IService {

	private StockDAO stockDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		stockDAO = (StockDAO) DAOFactory.getDAO(sessionFactory, StockDAO.class);
	}

	public Page<ProductWarehouseStock> listPagination(
			Page<ProductWarehouseStock> page, String searchType,
			List<String> searchValue) throws CRUDException {
		String ss = SearchUtil
				.getString(new String[] { "warehouse.name", "product.name" },// 高级查询条件
						new String[] { SearchUtil.STRING_LIKE,
								SearchUtil.BETWEEN_DATE },// 查询类型
						searchType,// 与或类型
						searchValue);// 查询值
		return stockDAO.pagination(page,
				"from ProductWarehouseStock where 1=1 " + ss,
				"warehouse.id,product.id");
	}

	public Page<ProductWarehouseStock> listPaginationBack(
			Page<ProductWarehouseStock> page, String searchType,
			List<String> searchValue) throws CRUDException {
		String ss = SearchUtil
				.getString(new String[] { "warehouse.name", "product.name" },// 高级查询条件
						new String[] { SearchUtil.STRING_LIKE,
								SearchUtil.BETWEEN_DATE },// 查询类型
						searchType,// 与或类型
						searchValue);// 查询值
		return stockDAO.pagination(page,
				"from ProductWarehouseStock where backAmounts > 0 " + ss,
				"warehouse.id,product.id");
	}

	public void save(ProductWarehouseStock object) throws CRUDException {
		List<ProductWarehouseStock> list = stockDAO.find(
				" from ProductWarehouseStock where product.id = '"
						+ object.getProduct().getId()
						+ "' and warehouse.id = '"
						+ object.getWarehouse().getId() + "'", null);
		if (list != null && list.size() > 0) {
			ProductWarehouseStock temp = list.get(0);
			temp.setAmounts(object.getAmounts() + temp.getAmounts());
			stockDAO.update(temp);
		} else
			stockDAO.save(object);
	}

	public void saveInStock(InOrderProduct iop) throws CRUDException {
		ProductWarehouseStock pws = stockDAO.findUniqueByProperty(
				"inOrderProduct", iop);
		if (pws == null) {
			pws = new ProductWarehouseStock();
		}
		pws.setAmounts(iop.getInAmounts());
		pws.setInOrder(iop.getInOrder());
		pws.setInOrderProduct(iop);
		pws.setProduct(iop.getProduct());
		pws.setWarehouse(iop.getWareHouse());
		stockDAO.saveOrUpdate(pws);
	}

	public void saveInStockForBack(InOrderProduct iop) throws CRUDException {
		ProductWarehouseStock pws = iop.getSalesBackOrderProduct()
				.getSalesOrderProduct().getStock();
		pws.setAmounts(pws.getAmounts() + iop.getInAmounts());
		pws.setBackAmounts(iop.getInAmounts());
		stockDAO.saveOrUpdate(pws);
	}

	public void update(ProductWarehouseStock object) throws CRUDException {
		ProductWarehouseStock pws = findById(object.getId());
		if (pws.getWarehouse().getId().equals(object.getWarehouse().getId())) {
			pws.setAmounts(object.getAmounts());
			stockDAO.update(pws);
		} else {
			List<ProductWarehouseStock> list = stockDAO.find(
					" from ProductWarehouseStock where product.id = '"
							+ object.getProduct().getId()
							+ "' and warehouse.id = '"
							+ object.getWarehouse().getId() + "'", null);
			if (list != null && list.size() > 0) {
				ProductWarehouseStock temp = list.get(0);
				temp.setAmounts(object.getAmounts() + temp.getAmounts());
				stockDAO.update(temp);
				stockDAO.delete(object);
			}
		}
	}

	public ProductWarehouseStock findById(Serializable id) throws CRUDException {
		return stockDAO.get(id);
	}

	public void update(OutOrderProduct ooProduct) throws CRUDException {
		String hql = "from ProductWarehouseStock where 	product.id = '"
				+ ooProduct.getProduct().getId() + "'";
		if (!ooProduct.getWarehouse().getId().equals("undefined")) {
			hql += " and warehouse.id = '" + ooProduct.getWarehouse().getId()
					+ "'";
		}
		List<ProductWarehouseStock> list = stockDAO.find(hql);
		if (list != null && list.size() > 0) {
			ProductWarehouseStock temp = list.get(0);
			temp.setAmounts(temp.getAmounts() - ooProduct.getAmounts());
			stockDAO.update(temp);
		}

		/*
		 * ProductWarehouseStock temp =
		 * stockDAO.get(ooProduct.getProductWarehouseStock().getId());
		 * temp.setAmounts(temp.getAmounts() - ooProduct.getAmounts());
		 * stockDAO.update(temp);
		 */
	}
}
